<template>
  <el-container>
    <el-header>
      <!--导航条 -->
      <Data_navigate></Data_navigate>
    </el-header>
    <el-main>
      <el-container>
        <el-container>
          <el-header>
            <!--普通查询条件  isShowQueryPlus是否展示高级查询-->
            <Data_querys placeholder="名称" :isShowQueryPlus="true">
            </Data_querys>
            <div class="farm2-data-buttons-operators">
              <el-button v-if="false" icon="Plus" type="primary" @click="openDataFormClick" plain>创建</el-button>
              <el-button icon="Delete" type="danger" @click="datasDeleteClick" plain>删除</el-button>
            </div>
          </el-header>
          <el-main>
            <!--数据集-->
            <div class="farm2-data-table">
              <el-table v-loading="loadingFlag" :data="dataResult.data" :stripe="true"
                @selection-change="handleSelectionChange" :highlight-current-row="true" class="table"
                @sort-change="(data: SortInfo) => { doDataQuery(tableUtils.getSortQuery(data)) }">
                <el-table-column fixed="left" type="selection" width="55" />
                <el-table-column sortable="custom" prop="TITLE" label="标题" min-width="450" />
                <el-table-column sortable="custom" prop="STATE" label="状态" min-width="100"
                  :formatter="dic.formatSTATE" />
                <el-table-column prop="FILENUM" label="附件数" min-width="80" />
                <el-table-column prop="VERSIONNUM" label="版本数" min-width="80" />
                <el-table-column sortable="custom" prop="KNOWMODEL" label="知识模型" min-width="120"
                  :formatter="dic.formatModel" />
                <el-table-column sortable="custom" prop="AUTHOR" label="原作者" min-width="120" />
                <el-table-column sortable="custom" prop="PUBTIME" label="发布时间" min-width="220"
                  :formatter="dic.formatTime" />
                <el-table-column sortable="custom" prop="CTIME" label="创建时间" min-width="220"
                  :formatter="dic.formatTime" />
                <el-table-column sortable="custom" prop="ETIME" label="修改时间" min-width="220"
                  :formatter="dic.formatTime" />
                <el-table-column sortable="custom" prop="CUSERNAME" label="创建人姓名" min-width="120" />
                <el-table-column sortable="custom" prop="EUSERNAME" label="修改人姓名" min-width="120" />
                <el-table-column sortable="custom" prop="SYNCID" label="同步关键字" min-width="120" />
                <el-table-column sortable="custom" prop="SNO" label="查询号" min-width="120" />
                <el-table-column sortable="custom" prop="VALIDITYS" label="有效期开始" min-width="120" />
                <el-table-column sortable="custom" prop="VALIDITYE" label="有效期结束" min-width="120" />
                <el-table-column fixed="right" label="操作" min-width="300">
                  <template #default="scope">
                    <el-button link type="primary" icon="View" size="small" @click="dataViewClick(scope.row)">
                      查看
                    </el-button>
                    <el-button link type="success" icon="Files" size="small" @click="openVersionFileWin(scope.row)">
                      版本
                    </el-button>
                    <el-button link type="success" icon="CollectionTag" size="small" @click="openTagWin(scope.row)">
                      标签
                    </el-button>
                    <el-button link type="success" icon="Paperclip" size="small" @click="openKnowFileWin(scope.row)">
                      附件
                    </el-button>
                    <el-button v-if="false" link type="primary" icon="Edit" size="small"
                      @click="dataEditClick(scope.row)">
                      修改
                    </el-button>
                    <el-button link type="danger" icon="Delete" size="small"
                      @click="dataDeleteClick(scope.row)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </el-main>
          <el-footer>
            <!--分页框-->
            <Data_page :remoteData="dataResult" />
          </el-footer>
        </el-container>
      </el-container>
    </el-main>
  </el-container>
  <!--高级查询表单-->
  <SkcKnow_form_query></SkcKnow_form_query>
  <!--数据实体表单-->
  <SkcKnow_form_data></SkcKnow_form_data>
  <!--知识附件-->
  <skcknowfile_page ref="knowFileRef"></skcknowfile_page>
  <!--知识版本-->
  <skcknowversion_page ref="knowVersionRef"></skcknowversion_page>
  <!--知识版本-->
  <skcKnowTag_page ref="knowTagRef"></skcKnowTag_page>
</template>
<script setup lang="ts">
import skcknowfile_page from '../skcknowfile/skcknowfile_page.vue';
import skcknowversion_page from '../skcknowversion/skcknowversion_page.vue'
import dic from './utils/dictinarys'
import Data_navigate from '@/components/datas/data_navigate.vue';
import Data_page from '@/components/datas/data_page.vue';
import { onBeforeMount, onBeforeUnmount, onMounted, provide, reactive, ref } from 'vue';
import farm2Request from '@/service/remoteRequests/Farm2Request';
import Data_querys from '@/components/datas/data_querys.vue';
import notice from '@/components/msg/FarmNotice';
import QueryUtils from '@/hook/farmQueryUtils'
import mitt from 'mitt';
import { PageEvent } from '@/types/commons/PageEvent';
import type { SortInfo } from '@/types/elementplus/SortInfo';
import type { DataQuery } from '@/types/commons/DataQuery';
import type { DataResult } from '@/types/commons/DataResult';
import tableUtils from '@/hook/farmTableUtils';
import SkcKnow_form_query from './components/skcknow_form_query.vue';
import SkcKnow_form_data from './components/skcknow_form_data.vue';
import { getCreatType, getUpdateType, getViewType, type FormParam } from '@/types/commons/FormType';
import Farm2Request from '@/service/remoteRequests/Farm2Request';
import { RequestTypeEnum } from '@/types/commons/RequestTypeEnum';
import { ElMessageBox } from 'element-plus';
import skcKnowTag_page from '../skcknowtag/skcknowtag_page.vue';
//---------------------------------------------------------
//路由信息：
//import skcknow_page from "@/views/system/skcknow/skcknow_page.vue";
// {
//     path: "skcknow",
//     name: "skcknow_page",
//     component: skcknow_page,
//     meta: { title: "知识" },
//   },
//---------------------------------------------------------
const knowTagRef = ref();
const knowFileRef = ref();
const knowVersionRef = ref();
const eventBus = mitt();//事件总线
const dataResult: DataResult = reactive(QueryUtils.getDefaultResult());//数据查询结果
const dataQuery: DataQuery = reactive({});//查询条件
const loadingFlag = ref(true);//加载中遮罩
const multipleSelection = ref([]);//当前选定行
const handleSelectionChange = (selection: []) => {
  multipleSelection.value = selection;//选定行事件
};
//---------------------------------------------------------
//---------------------------------------------------------

//打开知识附件窗口
const openKnowFileWin = (row: { ID: string }) => {
  knowFileRef.value.openWin(row);
}


//打开知识版本窗口
const openVersionFileWin = (row: { ID: string }) => {
  knowVersionRef.value.openWin(row);
}

//打开知识标签窗口
const openTagWin = (row: { ID: string }) => {
  knowTagRef.value.openWin(row);
}




/**
 * 后台检索查询--HTTP(POST)
 * @param query
 */
const doDataQuery = async (query?: DataQuery) => {
  farm2Request.search('api/skcknow/query', Object.assign(dataQuery, query), loadingFlag)
    .then((remoteResult: DataResult) => {
      Object.assign(dataResult, remoteResult);
    })
    .catch((msg: string) => {
      notice.byError(msg);
    });
}

/**
 * 后台批量删除--HTTP(DELETE)
 * @param ids
 */
const doDataDelete = (ids: string[]) => {
  Farm2Request.submit(
    'api/skcknow/batch',
    RequestTypeEnum.delete,
    loadingFlag, ids
  ).then(() => {
    doDataQuery();
  }).catch((msg: string) => {
    notice.byError(msg);
  });
}


/**
 * 打开数据更新窗口-事件
 * @param row 选定行数据
 */
const dataEditClick = (row: { ID: string }) => {
  eventBus.emit(PageEvent.data_form_open, { isOpen: true, type: <FormParam>{ type: getUpdateType(), id: row.ID, data: row } });
}

/**
 * 删除一条-事件
 * @param row 选定行数据
 */
const dataDeleteClick = (row: { ID: string }) => {
  ElMessageBox.confirm('确认删除?', { icon: 'QuestionFilled' })
    .then(() => {
      doDataDelete([row.ID]);
    }).catch(() => { });
}

/**
 * 删除多条-事件
 * @param row
 */
const datasDeleteClick = () => {
  const selection: { ID: string; }[] = multipleSelection.value;
  const ids = selection.map(item => item.ID);
  ElMessageBox.confirm('当前选定' + ids.length + '条数据,确认删除?', { icon: 'QuestionFilled' })
    .then(() => {
      doDataDelete(ids);
    }).catch(() => { });
}



/**
 * 打开数据查看窗口-事件
 * @param row 选定行数据
 */
const dataViewClick = (row: { ID: string }) => {
  eventBus.emit(PageEvent.data_form_open, { isOpen: true, type: <FormParam>{ type: getViewType(), id: row.ID, data: row } });
}


/**
 * 打开数据创建窗口-事件
 */
const openDataFormClick = () => {
  eventBus.emit(PageEvent.data_form_open, { isOpen: true, type: <FormParam>{ type: getCreatType() } });
}

//---------------------------------------------------------
//---------------------------------------------------------
onMounted(() => {
  //页面加载后调用默认查询事件
  doDataQuery();
  //事件注册
  eventBus.on(PageEvent.data_do_query, (query) => {
    doDataQuery(query as DataQuery);//数据查询方法
  })
});
onBeforeMount(() => {
  provide('eventBus', eventBus);//子组件注入事件总线
});
onBeforeUnmount(() => {
  eventBus.all.clear();//移除事件总线
})
</script>
